package com.andy.wan.android.mvp.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.andy.wan.android.mvp.bean.ProjectClassifyData;
import com.andy.wan.android.mvp.ui.fragment.ProjectListFragment;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

/**
 * @Author: luofan
 * @CreateDate: 2020/3/28 16:31
 * @Description:
 */
public class ProjectFragmentPagerAdapter extends FragmentPagerAdapter {

    public static final String DATA = "fragmentData";

    private List<ProjectClassifyData> mDataList;

    private FragmentManager mFragmentManager;

    private Context mContext;

    public ProjectFragmentPagerAdapter(@NonNull FragmentManager fm, Context context) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        mFragmentManager = fm;
        mContext = context;
    }

    public void setDataList(List<ProjectClassifyData> dataList) {
        mDataList = dataList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = mFragmentManager.getFragmentFactory().instantiate(mContext.getClassLoader(), ProjectListFragment.class.getName());
        Bundle bundle = new Bundle();
        bundle.putParcelable(DATA, mDataList.get(position));
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getCount() {
        if (mDataList != null) {
            return mDataList.size();
        }
        return 0;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mDataList.get(position).getName();
    }
}
